package com.yuerlive.rainbow.widget;

import android.content.Context;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.CoordinatorLayout;
import android.view.Gravity;
import android.view.View;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.utils.DeviceUtils;

/**
 * Created by Admin on 2017/5/20.
 */

public class CustomBottomSheetDialog {
    private BottomSheetDialog bottomSheetDialog;

    public CustomBottomSheetDialog(Context context) {
        this(context,0);
    }

    public CustomBottomSheetDialog(Context context, int style){
        bottomSheetDialog = new BottomSheetDialog(context,style){
            @Override
            public void show() {
                isShowing = true;
                super.show();
            }

            @Override
            public void hide() {
                isShowing = false;
                super.hide();
            }

            @Override
            public void dismiss() {
                isShowing = false;
                super.dismiss();
            }

            @Override
            public boolean isShowing() {
                return isShowing;
            }

            private boolean isShowing;
        };
    }
    public BottomSheetDialog getInstance(View contentView){
        bottomSheetDialog.setContentView(contentView);
        View parent = (View) contentView.getParent();
        BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
        contentView.measure(0, 0);
        behavior.setPeekHeight(contentView.getMeasuredHeight());
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
        params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
        parent.setLayoutParams(params);
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        return  bottomSheetDialog;
    }
    public BottomSheetDialog getInstance2(View contentView){
        bottomSheetDialog.setContentView(contentView);
        View parent = (View) contentView.getParent();
        BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
        behavior.setPeekHeight(DeviceUtils.dp2px(contentView.getContext(),320));
        behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
        params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
        parent.setLayoutParams(params);
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        return  bottomSheetDialog;
    }
}
